package com.decent.ejfadmin.finance.dao;

import com.decent.ejfadmin.common.bean.OtherpayConfig;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * 代付配置
 *
 * @author weicx
 * @date 2019/02/25
 */
@Repository
@Mapper
public interface OtherpayConfigDao {
    /**
     * 查询代付配置
     *
     * @return
     */
    @Select("SELECT " +
            "id, " +
            "otherpay, " +
            "`status`, " +
            "quota, " +
            "createTime, " +
            "updateTime, " +
            "remark " +
            "FROM " +
            "yuhuang_otherpay_config")
    List<OtherpayConfig> queryOtherpayConfigs();

    /**
     * 查询代付当日提卡总额
     *
     * @param otherpay 代付账号
     * @return
     */
    @Select("SELECT IFNULL(SUM(price),0) FROM `yuhuang_withdraw_record` WHERE payBank=#{otherpay} AND to_days(endTime) = to_days(now()) and state!=3")
    BigDecimal getOtherpayWithdrawPriceSum(@Param("otherpay") String otherpay);

    /**
     * 修改限额
     *
     * @param otherpayConfig 代付配置
     * @return
     */
    @Update("update yuhuang_otherpay_config set quota=#{otherpayConfig.quota},updateTime=now() where id=#{otherpayConfig.id} ")
    int updateOtherpayConfig(@Param("alipayConfig") OtherpayConfig otherpayConfig);

    /**
     * 新增代付配置
     *
     * @param otherpayConfig 代付配置
     * @return
     */
    @Insert("insert into yuhuang_otherpay_config (otherpay,quota,createTime,updateTime,status) values(#{otherpayConfig.otherpay},#{otherpayConfig.quota},now(),now(),0)")
    int insertOtherpayConfig(@Param("otherpayConfig") OtherpayConfig otherpayConfig);

    /**
     * 修改代付配置状态
     *
     * @param status 状态
     * @param id     id
     * @return
     */
    @Update("update yuhuang_otherpay_config set status=#{status},updateTime=now() where id=#{id}")
    int updateStatus(@Param("status") Integer status, @Param("id") Integer id);

    /**
     * 查询代付
     *
     * @param id id
     * @return
     */
    @Select("select otherpay from yuhuang_otherpay_config where id=#{id}")
    String getOpenOtherpay(@Param("id") Integer id);

    /**
     * 关闭所有代付
     *
     * @return
     */
    @Update("update yuhuang_otherpay_config set status=0")
    int updateAllStatus();


}
